from utils.excel_util import ExcelUtil
from model.sysMenuModel import SysMenu
from schema.sysMenuSchema import SysMenuCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO
from datetime import datetime


class SysMenuExcel:
    # Define export columns
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [

        {"field": "menu_name", "title": "菜单名称", "required": True},

        {"field": "parent_id", "title": "父菜单ID", "required": False},

        {"field": "order_num", "title": "显示顺序", "required": False},

        {"field": "path", "title": "路由地址", "required": False},

        {"field": "component", "title": "组件路径", "required": False},

        {"field": "query", "title": "路由参数", "required": False},

        {"field": "route_name", "title": "路由名称", "required": False},

        {"field": "is_frame", "title": "是否为外链（0是 1否）", "required": False},

        {"field": "is_cache", "title": "是否缓存（0缓存 1不缓存）", "required": False},

        {"field": "menu_type", "title": "菜单类型（M目录 C菜单 F按钮）", "required": False},

        {"field": "visible", "title": "菜单状态（0显示 1隐藏）", "required": False},

        {"field": "status", "title": "菜单状态（0正常 2删除）", "required": False},

        {"field": "use_flag", "title": "菜单状态（0正常 1停用）", "required": False},

        {"field": "perms", "title": "权限标识", "required": False},

        {"field": "icon", "title": "菜单图标", "required": False},

        {"field": "create_by", "title": "创建者", "required": False},

        {"field": "create_time", "title": "创建时间", "required": False},

        {"field": "update_by", "title": "更新者", "required": False},

        {"field": "update_time", "title": "更新时间", "required": False},

        {"field": "remark", "title": "备注", "required": False},

    ]

    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [

            {"field": "menu_name", "title": "菜单名称"},

            {"field": "parent_id", "title": "父菜单ID"},

            {"field": "order_num", "title": "显示顺序"},

            {"field": "path", "title": "路由地址"},

            {"field": "component", "title": "组件路径"},

            {"field": "query", "title": "路由参数"},

            {"field": "route_name", "title": "路由名称"},

            {"field": "is_frame", "title": "是否为外链（0是 1否）"},

            {"field": "is_cache", "title": "是否缓存（0缓存 1不缓存）"},

            {"field": "menu_type", "title": "菜单类型（M目录 C菜单 F按钮）"},

            {"field": "visible", "title": "菜单状态（0显示 1隐藏）"},

            {"field": "status", "title": "菜单状态（0正常 2删除）"},

            {"field": "use_flag", "title": "菜单状态（0正常 1停用）"},

            {"field": "perms", "title": "权限标识"},

            {"field": "icon", "title": "菜单图标"},

            {"field": "create_by", "title": "创建者"},

            {"field": "create_time", "title": "创建时间"},

            {"field": "update_by", "title": "更新者"},

            {"field": "update_time", "title": "更新时间"},

            {"field": "remark", "title": "备注"},

        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """Get import template"""
        example_data = [{

            "menu_name": "example",

            "parent_id": 1,

            "order_num": 1,

            "path": "example",

            "component": "example",

            "query": "example",

            "route_name": "example",

            "is_frame": 1,

            "is_cache": 1,

            "menu_type": "A",

            "visible": "A",

            "status": "A",

            "use_flag": "A",

            "perms": "example",

            "icon": "example",

            "create_by": "example",

            "create_time": datetime.now(),

            "update_by": "example",

            "update_time": datetime.now(),

            "remark": "example",

        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="菜单权限表导入模板"
        )

    @classmethod
    async def export_data(cls, data: List[Any]) -> BytesIO:
        """Export data"""
        return await ExcelUtil.export_data(
            data=data,
            columns=cls.get_export_columns(),
            sheet_name="菜单权限表数据"
        )

    @classmethod
    def parse_import_data(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """Parse import data"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=SysMenuCreate
        )